package com.gylang.crawler.crawler.dy2018;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.gylang.crawler.domain.CrawlerErrorInfo;
import com.gylang.crawler.domain.Dy2018MovieSimpleInfo;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.selector.Selectable;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author gylang
 * data 2021/5/27
 */
@Component
public class Dy2018CrawlerPageProcessor extends BaseDy2018PageProcessor {

    @Autowired
    private ElasticsearchRestTemplate restTemplate;

    @Override
    public void process(Page page) {

        // 处理数据
        Selectable movieIntroHtml = page.getHtml().xpath("//*table[@class='tbspan']");


        try {
            BulkRequest bulkRequest = new BulkRequest();
            for (Selectable node : movieIntroHtml.nodes()) {
                String link = movieIntroHtml.nodes().get(0).xpath("[@class='ulink' and @title]/@href").toString();

                String id = ReUtil.get(NUMBER_P, link, 1);
                String introStr = node.toString();
                String date = ReUtil.get(DATE_P, introStr, 1);
                String movieName = ReUtil.get(MOVIE_NAME_P, introStr, 1);
                List<String> type = StrUtil.splitTrim(ReUtil.get(TYPE_P, introStr, 1), "/");
                List<String> alias = StrUtil.splitTrim(ReUtil.get(ALIAS_NAME_P, introStr, 1), "/");
                List<String> actor = StrUtil.splitTrim(ReUtil.get(ACTOR_P, introStr, 1), BLANK_7);
                List<String> direct = StrUtil.splitTrim(ReUtil.get(DIRECT_P, introStr, 1), BLANK_7);
                List<String> label = StrUtil.splitTrim(ReUtil.get(LABEL_P, introStr, 1), ",");
                Dy2018MovieSimpleInfo movieSimpleInfo = Dy2018MovieSimpleInfo.builder()
                        .actor(actor)
                        .datePublished(LocalDateTimeUtil.parseDate(date))
                        .alias(alias)
                        .id(id)
                        .direct(direct)
                        .name(movieName)
                        .type(type)
                        .label(label)
                        .build();


                IndexRequest indexRequest = new IndexRequest("dy_movie", "doc", id);
                indexRequest.source(JSON.toJSONString(movieSimpleInfo), XContentType.JSON);
                bulkRequest.add(indexRequest);
            }
            restTemplate.getClient().bulk(bulkRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            IndexRequest indexRequest = new IndexRequest("crawler_error", "doc");
            CrawlerErrorInfo error = CrawlerErrorInfo.builder()
                    .url(page.getUrl().toString())
                    .errorMsg(e.getMessage())
                    .errorTime(LocalDateTime.now())
                    .title("爬取dy2018")
                    .type("分类列表电影简介")
                    .errorType("解析失败")
                    .build();
            indexRequest.source(JSON.toJSONString(error), XContentType.JSON);
            e.printStackTrace();
        }

    }
}
